
#define OOE_BUTTON_WIDTH	16
#define OOE_BUTTON_HEIGHT	16

#import "ScrollViewDeluxeForOOE.h"
#import "wooddoc.h"
#import "ooe_server.h"

@implementation View (OOE_GUI)

- ooeStatusChanged:sender
{
	[subviews makeObjectsPerform:_cmd with:sender];
	return self;
}

@end


@interface ScrollViewOOEButton:Button
@end

@implementation ScrollViewOOEButton

- initFrame: (const NXRect *)rect for:view
{
	[self initFrame: rect];
	[self setAction: @selector(ooeSelected:)];
	[self setTarget: view];
	[self setBordered:NO];
	[self setIconPosition:NX_ICONONLY];
	[self setImage: [NXImage findImageNamed: "OOE_ScrollerButton"]];
	[self setAltImage: [NXImage findImageNamed: "OOE_ScrollerButtonH"]];
	[self setType: NX_MOMENTARYCHANGE];
	[self setFrame: rect];	
	[self setEnabled: NO];		
	return self;
}

- ooeStatusChanged:sender
{
	[self setEnabled: [sender isOOEServer]];
	if(![self isEnabled])
		[self setImage: [NXImage findImageNamed: "OOE_ScrollerButtonD"]];
	else {
		if([window isDocEdited])
			[self setImage: [NXImage findImageNamed: 
						"OOE_ScrollerButtonE"]];
		else 
			[self setImage: [NXImage findImageNamed: 
						"OOE_ScrollerButton"]];
	}
	return self;
}

@end

@implementation ScrollViewDeluxeForOOE

- theOOEButton
{
	return theOOEButton;
}

- initFrame: (const NXRect *)rect
{
	NXRect ooeButtonRect 
		= {0, 0, OOE_BUTTON_WIDTH, OOE_BUTTON_HEIGHT};
	
	[super initFrame:rect];
	
    // ooe button
    
	theOOEButton = [[ScrollViewOOEButton allocFromZone:[self zone]] 
					initFrame: &ooeButtonRect
					for:self];
	[self addSubview: theOOEButton];
	
	[self setVertScrollerRequired:YES];
	[self setHorizScrollerRequired:YES];
	
	[self addVertScrollerView:theOOEButton at:0];
	
	[self tile];
	
	return self;
}

- ooeSelected:sender
{
	int altEvent;
	id doc = [window delegate];
	
	altEvent = (([NXApp currentEvent]->flags & NX_ALTERNATEMASK) > 0 
					? 1 : 0);
	
 	if([window isDocEdited]){
		 [doc save:nil];
 	} else {
		[doc orderClientDocumentFront:nil];
	}
	
	if(!altEvent && ![window isDocEdited])
		[window performClose:nil];
		
	return self;
}


/*
- tile
{
	NXRect vScrollRect;
	NXRect ooeButtonRect;
	
	[super tile];

   	[vScroller getFrame: &vScrollRect];
 	[theOOEButton getFrame: &ooeButtonRect];
	
    // vertical
    
    // ooe button
    
	NX_X(&ooeButtonRect) = NX_X(&vScrollRect)+1;
	NX_Y(&ooeButtonRect) = NX_Y(&vScrollRect);

	[theOOEButton setFrame:&ooeButtonRect];

    // v scroller
    
	NX_HEIGHT(&vScrollRect) = NX_HEIGHT(&vScrollRect) - 
					(NX_HEIGHT(&ooeButtonRect));
	NX_Y(&vScrollRect) = NX_MAXY(&ooeButtonRect);
	[vScroller setFrame:&vScrollRect];

	return self;
}
*/

@end

